package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.AuthorMapper;
import com.heima.article.service.AuthorService;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojo.ApAuthor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 用户作者信息
 */
@Service
@Transactional
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper,ApAuthor> implements AuthorService {
    /**
     * 查询用户信息
     * @param userId
     * @return
     */
    @Override
    public ApAuthor findByUserId(Integer userId) {
        //检查参数
        if(userId==null ||userId==0){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据ID查询作者
        ApAuthor author = getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getId,userId));
        if(author==null){
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return author;
    }


    /**
     * 保存用户信息
     * @param apAuthor
     * @return
     */
    @Override
    public ResponseResult insert(ApAuthor apAuthor) {
        //检查参数
        if(apAuthor==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.保存作者
        save(apAuthor);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
